package com.cci.kangdao.dao;

import com.cci.kangdao.model.WorkOrderInfo;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;
import java.util.Map;

@MapperScan
public interface WorkOrderListDao {

    //根据新工单id获取所有旧工单信息
    @Select("with digui as(\n" +
            "select  a.ID,a.TransferTime,a.TransferReason,a.RelOrderIdOld from WorkOrder_T a where a.ID=#{newOrderId}\n" +
            "union all \n" +
            "select  WorkOrder_T.ID,WorkOrder_T.TransferTime,WorkOrder_T.TransferReason,WorkOrder_T.RelOrderIdOld from digui\n" +
            "inner join WorkOrder_T on digui.RelOrderIdOld= WorkOrder_T.ID)\n" +
            "select * from digui where digui.ID !=#{newOrderId}  order by digui.TransferTime desc  ")
    List<Map<String, Object>> getOldWorkOrderListByNewOrderId(long newOrderId);

    /**
     * 获取我的收藏列表
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getCollectionList(Map<String, Object> map);

    /**
     * 设置收藏
     *
     * @param map
     * @return
     */
    public int setCollection(Map<String, Object> map);

    /**
     * 某一账户取消收藏
     *
     * @param map
     * @return
     */
    public int cancelCollection(Map<String, Object> map);

    /**
     * 账户与工单的目前关联状态
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public Map<String, Object> getCollectionStatus(Map<String, Object> map);

    /**
     * 客户查看工单详情
     *
     * @param map
     * @return
     */
    List<WorkOrderInfo> getCustomerOrderList(Map<String, Object> map);


    //客户查看工单列表   优化逻辑
    List<WorkOrderInfo> getNewCustomerOrderList(Map<String, Object> map);


    /**
     * 服务技师查看工单详情
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getTechnicianOrderList(Map<String, Object> map);

    /**
     * 服务技师查看工单列表
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getNewTechnicianOrderList(Map<String, Object> map);

    /**
     * 服务站长查看工单详情
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getStationmasterOrderList(Map<String, Object> map);

    /**
     * 服务站长查看工单列表   将审核不通过的，并且不是自己创建的删除
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getNewStationmasterOrderListIsCreat(Map<String, Object> map);


    /**
     * 查询区域服务经理账户下的服务站有哪些
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<Map<String, Object>> getManagerHasStations(Map<String, Object> map);

    /**
     * 获取渠道下服务站信息
     *
     * @param map 通过companyId，Level，UserPhone
     * @return 管辖服务信息列表
     */
    @SuppressWarnings("all")
    List<Map<String, Object>> getChannelStationsByEngineerPhone(Map<String, Object> map);

    /**
     * 查询区域服务经理的工单详情
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getManagerOrderList(Map<String, Object> map);


    /**
     * 查询区域服务经理的工单列表
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getNewManagerOrderList(Map<String, Object> map);


    /**
     * 查询区域工程师的工单详情
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getDistrictEngineerOrderList(Map<String, Object> map);


    /**
     * 查询区域工程师的工单列表
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getNewDistrictEngineerOrderList(Map<String, Object> map);

    /**
     * 查询服务工单经理的工单列表
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getWorkOrderManagerOrderList(Map<String, Object> map);

    /**
     * 根据工单id查询服务工单经理的工单详情
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getWorkOrderManagerOrderById(Map<String, Object> map);


    /**
     * 查询工单操作记录表
     *
     * @param map
     * @return
     */
	/*@Select("select wof.OrderId,wof.processStatus,wof.CreateTime,wo.ServiceStationId,ss.StationName,ssr.HotlineTel,\n" +
			"ut.Contact as UserName,ut.Phone,wof.processStatus,wof.ProcessComment,wo.IsOutside,wo.GuessDistance,\n" +
			"case when wof.Creator = -2 then 1 else 0 end as IsOverTime,ISNULL(wo.RefuseReasonStation,'') as RefuseReasonStation," +
			"ISNULL(wo.CloseReason,'') as CloseReason,wof.Creator,ut2.Contact as CreatorContact,ut2.Phone as CreatorPhone\n" +
			"from WorkOrderFlow_T wof\n" +
			"left join WorkOrder_T wo on wof.OrderId = wo.ID\n" +
			"left join ServiceStation_T ss on ss.ID = wo.ServiceStationId\n" +
			"left join SSRepair_T ssr on ssr.ServiceStationID = ss.ID and wo.OrderChannel = ssr.CompanyID and ssr.Flag = 0\n" +
			"left join User_T ut on ut.ID = wof.ProcessorId\n" +
			"left join User_T ut2 on ut2.ID = wof.Creator\n" +
			"where OrderId = #{OrderId}\n" +
			"and not exists (select 1 from WorkOrderFlow_T wftt where wof.processStatus=wftt.processStatus and wof.ID<wftt.ID and wftt.OrderId = #{OrderId} and wftt.processStatus in(10,11))\n" +
			" order by wof.CreateTime")*/
    //把上面的sql改了，去掉出发和到达状态重复的限制，因为异常关闭申请可以在出发和到达触发
    @Select("select wof.OrderId,wof.processStatus,wof.CreateTime,wof.UpdateTime,wo.ServiceStationId,ss.StationName,ssr.HotlineTel,\n" +
            "ut.Contact as UserName,ut.Phone,wof.processStatus,wof.ProcessComment,wo.IsOutside,wo.GuessDistance,\n" +
            "case when wof.Creator = -2 then 1 else 0 end as IsOverTime,ISNULL(wo.RefuseReasonStation,'') as RefuseReasonStation," +
            "ISNULL(wo.CloseReason,'') as CloseReason,wof.Creator,ut2.Contact as CreatorContact,ut2.Phone as CreatorPhone\n" +
            "from WorkOrderFlow_T wof\n" +
            "left join WorkOrder_T wo on wof.OrderId = wo.ID\n" +
            "left join ServiceStation_T ss on ss.ID = wo.ServiceStationId\n" +
            "left join SSRepair_T ssr on ssr.ServiceStationID = ss.ID and wo.OrderChannel = ssr.CompanyID and ssr.Flag = 0\n" +
            "left join User_T ut on ut.ID = wof.ProcessorId\n" +
            "left join User_T ut2 on ut2.ID = wof.Creator\n" +
            "where OrderId = #{OrderId}\n" +
            "order by wof.CreateTime,wof.ID")
    public List<Map<String, Object>> getWorkOrderFlowTimeList(Map<String, Object> map);

    @SuppressWarnings("all")
    public Map<String, Object> getMaxOrderId(String time);

    /**
     * 查询客户服务经理的工单详情
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getCusManagerOrderList(Map<String, Object> map);

    /**
     * 查询客户服务经理的工单列表
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getNewCusManagerOrderList(Map<String, Object> map);

    /**
     * 查询商务处主任的工单详情
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getDepartmentOrderList(Map<String, Object> map);


    /**
     * 查询商务处主任的工单列表
     *
     * @param map
     * @return
     */
    @SuppressWarnings("all")
    public List<WorkOrderInfo> getNewDepartmentOrderList(Map<String, Object> map);


    //移动技师获取服务工单详情
    @Select("select a.OrderNoCrm,a.SellType,a.RefuseReasonStation,a.WarrantyStartDate,a.IsOutside,b.PositionLat,b.PositionLon,c.SMN,\n" +
            "case when (select count(ID) from SceneTechnicalConfirmation_T stct where stct.OrderId = a.ID and stct.Flag = 0) = 0 then 0 else 1 end as IsHaveSceneTechnicalConfirmationMsg,\n" +
            "case when (select count(ID) from EscalationOrder_T eot where eot.WorkOrderId = a.ID and eot.Flag = 0) = 0 then 0\n" +
            "else (select ID from EscalationOrder_T eot where eot.WorkOrderId = a.ID and eot.Flag = 0) end as EscalationOrderID,a.CustomerPhone,a.OrderChannel,a.ServiceStationId StationId,\n" +
            "a.CustomerName,a.LocLat,a.Location,a.LocLon,a.UpdateTime TimeStamp,\n" +
            "ISNULL(a.ChassisNumber,'') as vehicleCompany,dt2.DValue as ProblemLevel,dt.DValue as InvalidModel,dct.FaultSolution,\n" +
            "case when (select count(id) from WorkOrderComm_T where OrderId = a.ID)> 0 then 0 else 1 end isUserAppraise,\n" +
            "ISNULL(a.WorkHours,'') as WorkHours,ISNULL(a.WorkMileages,'')  as WorkMileages,a.OrderType,a.OrderSource,a.ESN,\n" +
            "case when (select count(wosd.ID) from WorkOrderSelfDiagnostic_T wosd where wosd.WorkOrderId = a.ID) = 0 then 0 else 1 end isHaveselfDiagnosticDetail,\n" +
            "a.OrderNo,a.OrderStatus OrderStatusNo,ISNULL(c.MarketType,'') as MarketType,a.CreateTime,a.CustomerComplaint,\n" +
            "(select DValue from Dictionaries_T where DTableName='OrderStatus' and\n" +
            "\t\tDInt=(case when 0=0 then case when a.OrderStatus=30 then 15 else a.OrderStatus end else a.OrderStatus end)) OrderStatus,\n" +
            "case when a.WorkHours is not null and a.WorkHours != '' then a.WorkHours+'h' else a.WorkMileages+'km' end WorkHoursMileages,\n" +
            "ISNULL(c.Application,'') as Application,a.ID as OrderId\n" +
            "from WorkOrder_T a\n" +
            "inner join EngineMsg c on a.ESN = c.ESN\n" +
            "left join ServiceStation_T b on a.ServiceStationId = b.ID\n" +
            "left join DiagnoseConclusion_T dct on dct.OrderId = a.ID\n" +
            "left join Dictionaries_T dt on dct.InvalidModel = dt.DInt and dt.Flag=0 and dt.DTableName='InvalidModel'\n" +
            "left join Dictionaries_T dt2 on dct.ProblemLevel = dt2.DInt and dt2.Flag=0 and dt2.DTableName='ProblemLevel'\n" +
            "where a.ID = #{orderId}")
    Map<String, Object> getMTOrderInfo(@Param("orderId") long orderId);


    //外部系统终端用户获取工单详情
    @Select("select wt.ID OrderId,wt.OrderChannel,wt.OrderNo,wt.CreateTime,wt.CustomerComplaint,wt.CustomerPhone,wt.ESN,wt.CustomerName,wt.OrderStatus OrderStatusNo,wt.UpdateTime TimeStamp,wt.OrderType,\n" +
            "(select DValue from Dictionaries_T where DTableName='OrderStatus' and \n" +
            "DInt=(case when 0=0 then case when wt.OrderStatus in (2,3,5,18,20,23,24,33) then 4 when wt.OrderStatus=3 then (case when wt.TransferReason like '%用户不需要维修%' then 15 else 4 end)\n" +
            "when wt.OrderStatus=9 then 7 when wt.OrderStatus=21 then 16 when wt.OrderStatus=30 then 15 when\n" +
            "wt.OrderStatus=31 then (select top 1 processStatus from WorkOrderFlow_T where OrderId = wt.ID and CreateTime < wt.UpdateTime order by CreateTime desc)\n" +
            "else wt.OrderStatus end else wt.OrderStatus end)) OrderStatus,\n" +
            "wt.LocLon,wt.LocLat,sst.PositionLat,sst.PositionLon,em.EngineType,wt.IsOutside,wt.ServiceStationId StationId,convert(float,round(wt.GuessDistance,1)) as GuessDistance,\n" +
            "case when (select count(id) from WorkOrderComm_T where OrderId = wt.ID)> 0 then 0 else 1 end isUserAppraise,\n" +
            "wt.OrderSource,wt.ContactName,wt.ContactPhone,ISNULL(wt.CustomerCompanyName,'') as CustomerCompanyName\n" +
            "from WorkOrder_T wt \n" +
            "left join ServiceStation_T sst on wt.ServiceStationId=sst.ID\n" +
            "left join EngineMsg em on wt.EngineMsgId=em.ID\n" +
            "where  RelOrderId is null and wt.ID=#{orderId}")
    Map<String, Object> outSideUserGetOrderInfoById(@Param("orderId") long orderId);


    /**
     * 3.1.0
     * 根据服务工单id查询出服务工单的异常关闭时间
     */
    @MapKey("OrderId")
    Map<Long, Map<String, Object>> getExceptionCloseTimeByOrderIds(@Param("list") List<Long> orderIds);


    /**
     * 3.1.0
     * 根据服务工单id查询出服务工单的技师维修完成时间
     * 参数说明：
     * orderIds为服务工单为待用户评价或服务完成的服务工单id集合
     */
    @SuppressWarnings("all")
    List<Map<String, Object>> getRepairFinishedTimeByOrderIds(@Param("list") List<Long> orderIds);


    /**
     * 3.1.0
     * 根据服务工单id查询出服务工单的技师维修完成时间
     * orderIds为服务工单为服务完成的服务工单id集合
     */
    @MapKey("OrderId")
    Map<Long, Map<String, Object>> getRepairFinishedTimeForServiceFinishedByOrderIds(@Param("list") List<Long> orderIds);

    List<WorkOrderInfo> getFavorateByOrderIdList(@Param("orderIdList") List<Long> orderIdList,@Param("userId") Long userId);

    @Select("select processStatus from WorkOrderFlow_T where Flag = 0 and OrderId = #{orderId}")
    public List<Integer> getprocessStatusByOrderId(@Param("orderId") Long orderId);

    List<WorkOrderInfo> getServiceStationByStationIdList(@Param("stationIdList") List<Long> stationIdList);

}
